package com.goldcn.user.controller;

import com.goldcn.common.consts.RestConst;
import com.goldcn.common.exception.ServiceException;
import com.goldcn.user.dto.SubjectInfoDto;
import com.goldcn.user.model.Homepage;
import com.goldcn.user.model.Subject;
import com.goldcn.user.service.HomepageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by 1111 on 2016/11/23.
 */
@Api(value = "首页数据及主题管理")
@RestController
public class HomepageController {
    @Autowired
    HomepageService homepageService;

    @ApiOperation(value = "添加主题 兼容小商城首页模板 . 2018")
    @RequestMapping(value = "/subject", method = RequestMethod.POST)
    public Object addSubject(@RequestBody SubjectInfoDto infoDto) throws Exception {
        return homepageService.addOrUpdateSubject(infoDto, 1);
    }

    @ApiOperation(value = "修改主题 兼容小商城首页模板 . 2018")
    @RequestMapping(value = "/subject", method = RequestMethod.PUT)
    public Object updateSubject(@RequestBody SubjectInfoDto infoDto) throws Exception {
        return homepageService.addOrUpdateSubject(infoDto, 2);
    }

    @ApiOperation(value = "删除主题 兼容小商城首页模板 . 2018")
    @RequestMapping(value = "/subject/{id}", method = RequestMethod.DELETE)
    public Object deleteSubject(@PathVariable Long id) {
        return homepageService.deleteSubject(id);
    }

    @ApiOperation(value = "按id查询主题详情")
    @RequestMapping(value = "/subject/{id}", method = RequestMethod.GET)
    public Object selectSubjectByID(@PathVariable Long id) {
        return homepageService.selectSubjectInfo(id, null);
    }

    @ApiOperation(value = "按id查询主题详情v2")
    @RequestMapping(value = "/subjectV2/{id}/{userID}", method = RequestMethod.GET)
    public Object selectSubjectByIDv2(@PathVariable Long id, @PathVariable Long userID) {
        return homepageService.selectSubjectInfo(id, userID);
    }


    @ApiOperation(value = "查询所有主题列表")
    @RequestMapping(value = "/subject/{pageNum}/{pageSize}", method = RequestMethod.GET)
    public Object selectSubjectList(@PathVariable int pageNum, @PathVariable int pageSize) {
        return homepageService.selectSubjectList(pageNum, pageSize, null);
    }


    @ApiOperation(value = "查询模板1首页所有数据")
    @RequestMapping(value = "/homepage", method = RequestMethod.GET)
    public Object homepage() {
        if (true)
            throw new ServiceException("您当前版本太低，请更新后再使用.", RestConst.ErrorCode.SERVICE_FAIL);
        return homepageService.selectHomepgeAll(1, null);
    }

    @ApiOperation(value = "按id查询首页数据")
    @RequestMapping(value = "/homepage/{id}", method = RequestMethod.GET)
    public Object selectHomepageByID(@PathVariable Integer id) {
        return homepageService.selectHomepageByID(id);
    }

    @ApiOperation(value = "更新首页middle主题")
    @RequestMapping(value = "/homepageMiddle/{id}", method = RequestMethod.PUT)
    public Object updateHomepageMiddle(@RequestBody Subject subject, @PathVariable Integer id) {
        return homepageService.updateHomepageMiddle(subject, id);
    }

    @ApiOperation(value = "更新首页Banner主题")
    @RequestMapping(value = "/homepageBanner", method = RequestMethod.PUT)
    public Object updateHomepageBanner(@RequestBody JSONObject jsonObject) {
        return homepageService.updateHomepageBanner(jsonObject);
    }

    @ApiOperation(value = "更新首页更多专题项")
    @RequestMapping(value = "/homepageMoreTitle", method = RequestMethod.PUT)
    public Object updateHomepageMoreTitle(@RequestBody JSONObject jsonObject) {
        return homepageService.updateHomepageMoreTitle(jsonObject);
    }

    /**
     * -----------------------------------------模板2----------------------------------------------
     */


    @ApiOperation(value = "查询模板2首页所有数据")
    @RequestMapping(value = "/homepage2", method = RequestMethod.GET)
    public Object selectSubjectByID2() {
        return homepageService.selectHomepgeAll(2, null);
    }

    @ApiOperation(value = "查询模板2首页所有数据v2")
    @RequestMapping(value = "/homepage2/{userID}", method = RequestMethod.GET)
    public Object selectSubjectByIDv2(@PathVariable Long userID) {
        return homepageService.selectHomepgeAll(2, userID);
    }

    @ApiOperation(value = "查询默认模板(暂时只有1,2). 兼容小商城首页模板 . 2018")
    @RequestMapping(value = "/muban/{oID}", method = RequestMethod.GET)
    public Object getMuban(@PathVariable Long oID) {
        return homepageService.getMuban(oID);
    }

    @ApiOperation(value = "设置默认模板(暂时只有1,2)")
    @RequestMapping(value = "/muban/{oID}/{muban}", method = RequestMethod.PUT)
    public Object setMuban(@PathVariable Long oID, @PathVariable Integer muban) {
        return homepageService.setMuban(oID, muban);
    }

    @ApiOperation(value = "更新模板2的Banner和更多专题（type：1banner，2更多专题）")
    @RequestMapping(value = "/updateBM/{type}", method = RequestMethod.POST)
    public Object updateHomepageMuban2Banner(@PathVariable int type, @RequestBody JSONObject jsonObject) {
        return homepageService.updateHomepageMuban2Banner(jsonObject, type);
    }


    @ApiOperation(value = "更新模板2首页的middle主题数据(id 为10或者11)")
    @RequestMapping(value = "/updateUmiddle", method = RequestMethod.POST)
    public Object updateHomepageMuban2Middle(@RequestBody Homepage homepage) {
        return homepageService.updateHomepageMuban2Middle(homepage);
    }


    /**-----------------------------------------模板2----------------------------------------------*/


    /**
     * -----------------------------------------小商城版本专用 begin----------------------------------------------
     */
    @ApiOperation(value = "查询所有主题列表,小商城版本专用  2018")
    @RequestMapping(value = "/subject/{pageNum}/{pageSize}/{shopID}", method = RequestMethod.GET)
    public Object selectSubjectListShop(@PathVariable int pageNum, @PathVariable int pageSize, @PathVariable Long shopID) {
        return homepageService.selectSubjectList(pageNum, pageSize, shopID);
    }


    @ApiOperation(value = "按id查询主题详情v2,小商城版本专用  2018")
    @RequestMapping(value = "/subjectV2/shop/{id}/{shopID}", method = RequestMethod.GET)
    public Object selectSubjectByIDv2Shop(@PathVariable Long id, @PathVariable Long shopID) {
        return homepageService.selectShopSubjectInfo(id, shopID);
    }

    @ApiOperation(value = "更新模板2首页的middle主题数据(id 为10或者11),小商城版本专用  2018")
    @RequestMapping(value = "/updateUmiddle/{shopID}", method = RequestMethod.POST)
    public Object updateHomepageMuban2MiddleShop(@RequestBody Homepage homepage, @PathVariable Long shopID) {
        return homepageService.updateHomepageMuban2MiddleShop(homepage,shopID);
    }


    @ApiOperation(value = "更新模板2的Banner和更多专题（type：1banner，2更多专题），小商城版本专用  2018")
    @RequestMapping(value = "/updateBM/{type}/{shopID}", method = RequestMethod.POST)
    public Object updateHomepageMuban2BannerShop(@PathVariable int type, @RequestBody JSONObject jsonObject, @PathVariable Long shopID) {
        return homepageService.updateHomepageMuban2BannerShop(jsonObject, type, shopID);
    }

    @ApiOperation(value = "查询默认模板(暂时只有1,2). 兼容小商城首页模板 . 2018")
    @RequestMapping(value = "/mubanShop/{oID}", method = RequestMethod.GET)
    public Object getMubanShop(@PathVariable Long oID) {
        return homepageService.getMubanV2(oID);
    }


    @ApiOperation(value = "查询模板2首页所有数据")
    @RequestMapping(value = "/homepage2/v18/{shopID}", method = RequestMethod.GET)
    public Object selectSubjectByID2V18(@PathVariable Long shopID) {
        return homepageService.selectHomepgeAll(2, null, shopID);
    }


    @ApiOperation(value = "商品下架删除专题下的商品(小商城传shopID，公司传companyID)")
    @RequestMapping(value = "/subject/goodsShelve/{companyID}/{goodsID}", method = RequestMethod.GET)
    public Object deleteByCompanyIDAndGoodsID(@PathVariable Long companyID,@PathVariable Long goodsID) {
        return homepageService.deleteByCompanyIDAndGoodsID(companyID,goodsID);
    }
}
